package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1ItemStatus;

public class S_AddItem extends ServerBasePacket {
  private byte[] _byte;
  
  public S_AddItem(L1ItemInstance item) {
    this._byte = null;
    writeC(15);
    writeD(item.getId());
    switch (item.getItem().getItemId()) {
      case 40318:
        writeH(166);
        break;
      case 40319:
        writeH(569);
        break;
      case 40321:
        writeH(837);
        break;
      case 49158:
        writeH(3674);
        break;
      case 49157:
        writeH(3605);
        break;
      case 49156:
        writeH(3606);
        break;
      default:
        writeH(0);
        break;
    } 
    int type = item.getItem().getUseType();
    if (type < 0)
      type = 0; 
    writeC(type);
    if (item.getChargeCount() > 0) {
      writeC(item.getChargeCount());
    } else {
      writeC(0);
    } 
    writeH(item.get_gfxid());
    writeC(item.getBless());
    writeD((int)Math.min(item.getCount(), 2000000000L));
    writeC(item.getItemStatusX());
    writeS(item.getViewName());
    if (!item.isIdentified()) {
      writeC(0);
    } else {
      L1ItemStatus itemInfo = new L1ItemStatus(item);
      byte[] status = itemInfo.getStatusBytes(false).getBytes();
      writeC(status.length);
      byte[] array;
      int length = (array = status).length;
      int i = 0;
      while (i < length) {
        byte b = array[i];
        writeC(b);
        i++;
      } 
    } 
    writeC(10);
    writeH(0);
    writeD(0);
    writeD(0);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
